<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="users"></div>
</body>
<script>
    let webSocket = null;
    if('WebSocket' in window){
        webSocket = new WebSocket('ws://192.168.1.3:8080/webSocket');
    }else{
        alert('该浏览器不支持Websocket');
    }
    webSocket.onopen = function(e){
        console.log("建立连接");
    }
    webSocket.onclose = function(e){
        console.log('连接关闭');
    }
    webSocket.onmessage = function(e){
        console.log('收到消息：'+e.data);
        var userContrain = document.getElementById("users");
        userContrain.append(e.data);
    }
    webSocket.onerror = function(e){
        alert('websocket通讯发生错误');
    }
    // 窗口关闭时websocket关闭
    window.onbeforeunload = function () {
        webSocket.close();
    }

</script>
</html>